<?php
// TRACK IT
if(isset($_GET['id']) && isset($_GET['version']))
{
    $tag = $_GET['id']."_".$_GET['version'];
    if(isset($tag))
    {
        if ($db = new SQLiteDatabase('track.db')) {
            //Check if Tag Exists
            $q = @$db->query("SELECT hits FROM track WHERE name = '".$tag."';");
            if ($q === false)
            {
                $db->queryExec('CREATE TABLE track (id INTEGER PRIMARY KEY, name INTEGER, hits INTEGER);');
                $db->queryExec("INSERT INTO track (name, hits) VALUES ('".$tag."', 1);");
                $hits = 1;
            }
            else
            {
                $result = $q->fetchSingle();
                if(empty($result))
                {
                    $db->queryExec("INSERT INTO track (name, hits) VALUES ('".$tag."', 1);");
                }
                $hits = $result + 1;
            }
            $db->queryExec("UPDATE track SET hits = '$hits' WHERE name = '".$tag."';");
        }
        else
        {
            die($err);
        }
    }
}
elseif(isset($_GET['view']))
{
    if ($db = new SQLiteDatabase('track.db')) {
        $q = @$db->query("SELECT * FROM track;");
        if ($q === false)
        {
            echo "No Database";
        }
        else
        {
            $result = $q->fetchAll();
            print_r($result);
            die();
        }
    }
    else
    {
        die($err);
    }
}
header ("content-type: text/xml");
// REAL STUFF
$results = array();
$handler = opendir(".");
while ($file = readdir($handler))
{
    if ($file != '.' && $file != '..')
    {
        if(preg_match('/webos-(.*?)-ko.xpi/', $file))
        {
            $results[] = $file;
        }
    }
}
closedir($handler);

function cmp($filea, $fileb)
{
    $order = array("rc" => 3, "b" => 2, "a" => 1, "pre" => 0);
    // parse out the order and hardcode the other 0.0.0 part.
    if ($filea == $fileb)
        return 0;
    $filea_array = array();
    preg_match('/webos-(.*?)-ko.xpi/', $filea, $filea_array);
    $filea_numbers = array();
    preg_match('/([0-9]+).([0-9]+).([0-9]+)(([a-zA-Z]+)([0-9]+))?/', $filea_array[1], $filea_numbers);
    $fileb_array = array();
    preg_match('/webos-(.*?)-ko.xpi/', $fileb, $fileb_array);
    $fileb_numbers = array();
    preg_match('/([0-9]+).([0-9]+).([0-9]+)(([a-zA-Z]+)([0-9]+))?/', $fileb_array[1], $fileb_numbers);
    /*(
    [0] => 0.1.3a3
    [1] => 0
    [2] => 1
    [3] => 3
    [4] => a3
    [5] => a
    [6] => 3
    )*/
    // to lazy recode, do this when you have time!
    if(intval($filea_numbers[1]) != intval($fileb_numbers[1]))
    {
        if(intval($filea_numbers[1]) < intval($fileb_numbers[1]))
            return 1;
        else
            return -1;
    }
    if(intval($filea_numbers[2]) != intval($fileb_numbers[2]))
    {
        if(intval($filea_numbers[2]) < intval($fileb_numbers[2]))
            return 1;
        else
            return -1;
    }
    if(intval($filea_numbers[3]) != intval($fileb_numbers[3]))
    {
        if(intval($filea_numbers[3]) < intval($fileb_numbers[3]))
            return 1;
        else
            return -1;
    }
    if(!key_exists(5, $filea_numbers))
        return -1;
    if(!key_exists(5, $fileb_numbers))
        return 1;
    if($order[intval($filea_numbers[5])] != $order[intval($fileb_numbers[5])])
    {
        if($order[intval($filea_numbers[5])] < $order[intval($fileb_numbers[5])])
            return 1;
        else
            return -1;
    }
    if(intval($filea_numbers[6]) != intval($fileb_numbers[6]))
    {
        if(intval($filea_numbers[6]) < intval($fileb_numbers[6]))
            return 1;
        else
            return -1;
    }
    return -1;
}

usort($results, "cmp");

$version = preg_replace('/webos-(.*?)-ko.xpi/', '\1', $results[0]);
$v = array("version" => $version, "file" => $results[0]);

echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>'; ?>
<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <RDF:Description about="urn:mozilla:extension:webos@templarian.com">
    <em:updates>
      <RDF:Seq>

        <RDF:li>
          <RDF:Description>
            <em:version><?php echo $v['version']; ?></em:version>
            <em:targetApplication>
              <RDF:Description>
                <em:id>{b1042fb5-9e9c-11db-b107-000d935d3368}</em:id>
                <em:minVersion>4.4.0</em:minVersion>
                <em:maxVersion>6.*</em:maxVersion>
                <em:updateLink>https://templarian.com/addon/webos/<?php echo $v['file']; ?></em:updateLink>
                <em:updateHash>sha1:<?php echo sha1_file($v['file']); ?></em:updateHash>
                <em:updateInfoURL>http://webos.templarian.com/komodo/</em:updateInfoURL>
              </RDF:Description>
            </em:targetApplication>
            <em:targetApplication>
              <RDF:Description>
                <em:id>{36E66FA0-F259-11D9-850E-000D935D3368}</em:id>
                <em:minVersion>4.4.0</em:minVersion>
                <em:maxVersion>6.*</em:maxVersion>
                <em:updateLink>https://templarian.com/addon/webos/<?php echo $v['file']; ?></em:updateLink>
                <em:updateHash>sha1:<?php echo sha1_file($v['file']); ?></em:updateHash>
                <em:updateInfoURL>http://webos.templarian.com/komodo/</em:updateInfoURL>
              </RDF:Description>
            </em:targetApplication>
          </RDF:Description>
        </RDF:li>

      </RDF:Seq>
    </em:updates>
   </RDF:Description>
</RDF:RDF>